package com.dawn.module.hospital.util;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

/**
 * @description:
 * @author: cnsu-cmh
 * @time: 2024/8/8 11:46
 */
public class SecEncryptUtil {


    /**
     * 自定义 KEY
     */
    private static byte[] keybytes = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x50,
            0x37, 0x38, 0x39, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46 };

    /**
     * @Title: encrypt
     * @author yunlin.liu
     * @Description: 加密
     * @param @param value
     * @param @return    设定文件
     * @return String    返回类型
     * @throws
     */
    public static String encrypt(String value) throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {

        String s = null;

        int mode = Cipher.ENCRYPT_MODE;

        try {
            Cipher cipher = initCipher(mode);

            byte[] outBytes = cipher.doFinal(value.getBytes());

            s = String.valueOf(Hex.encodeHex(outBytes));
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        return s;
    }


    /**
     * @Title: initCipher
     * @author yunlin.liu
     * @Description: 初始化密码
     * @param @param mode
     * @param @return
     * @param @throws NoSuchAlgorithmException
     * @param @throws NoSuchPaddingException
     * @param @throws InvalidKeyException    设定文件
     * @return Cipher    返回类型
     * @throws
     */
    private static Cipher initCipher(int mode) throws NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        Key key = new SecretKeySpec(keybytes, "AES");
        cipher.init(mode, key);
        return cipher;
    }



    /**
     * @Title: decrypt
     * @author yunlin.liu
     * @Description: 解密
     * @param @param value
     * @param @return    设定文件
     * @return String    返回类型
     * @throws
     */
    public static String decrypt(String value) throws DecoderException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
        String s = null;

        int mode = Cipher.DECRYPT_MODE;

        try {
            Cipher cipher = initCipher(mode);

            byte[] outBytes = cipher
                    .doFinal(Hex.decodeHex(value.toCharArray()));

            s = new String(outBytes);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        return s;
    }

    /**
     * 加密
     * @param param
     * @return
     * @throws Exception
     */
    public static final String encryption(String param) throws Exception {
        return encrypt(param);
    }

    /**
     * 解密
     * @param value
     * @return
     * @throws Exception
     */
    public static final String decryption(String value) throws Exception {
        return decrypt(value);
    }
}
